﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace KOPLibrary.Forms.CashFlow
{
    public partial class AcoountRelationControl : UserControl
    {
        public AcoountRelationControl()
        {
            InitializeComponent();
        }


        private bool inited = false;
        private Guid requestedId = Guid.Empty;

        public void Init(BindingSource source, string member)
        {
            Init(source, member, Guid.Empty, Guid.Empty);
        }

        public void Init(BindingSource source, string member, Guid mainId, Guid accId)
        {
            bindingSource1.DataSource = source;
            bindingSource2.DataSource = bindingSource1;
            bindingSource2.DataMember = member;
            ctlMainAcc.ValueMember = "ID";
            ctlMainAcc.DisplayMember = "Ad";

            ctlAcc.ValueMember = "ID";
            ctlAcc.DisplayMember = "Ad";

            source.PositionChanged += new EventHandler(source_PositionChanged);
            inited = true;
            if (mainId != Guid.Empty)
            {
                SelectedParentID = mainId;
                SelectedID = accId;
            }

            
        }

        public object SelectedParentID
        {
            get
            {
                if (ctlMainAcc.SelectedValue != null)
                    return ctlMainAcc.SelectedValue;
                else return -1;
            }
            set
            {
                if (!inited)
                    return;
                if (!(value is DBNull) && ctlMainAcc.SelectedValue != value)
                    ctlMainAcc.SelectedValue = value;
                if (requestedId != Guid.Empty)
                {
                    SelectedID = requestedId;
                    requestedId = Guid.Empty;
                }

            }
        }

        public object SelectedID
        {
            get
            {
                if (ctlAcc.SelectedValue != null)
                    return ctlAcc.SelectedValue;
                else return -1;
            }

            set
            {
                if (!inited) return;

                if (!(value is DBNull))
                {
                    bool found = false;
                    foreach(DataRowView item in ctlAcc.Items) {
                        Guid id = (Guid)item["ID"];
                        if (id == (Guid)value)
                        {
                            found = true;
                            break;
                        }
                    }
                    if (found)
                        ctlAcc.SelectedValue = value;
                    else
                    {
                        requestedId = (Guid)value;
                    }
                }
            }
        }

        void source_PositionChanged(object sender, EventArgs e)
        {
            
        }

        private void AcoountRelationControl_Load(object sender, EventArgs e)
        {

        }

        private void ctlMainAcc_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void ctlAcc_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}
